from django.http import HttpResponse
from django.shortcuts import render,redirect
from index.models import Article
from index.form import LoginForm,searchForm,UserForm,UserRegForm
from django.urls import reverse
from django.contrib.auth import logout
from django.contrib.auth.decorators import login_required
# login_required 是一个装饰器，用于限制只有登录用户才能访问某些视图函数。
from django.conf import settings
import json
from index.utils.response import BadRequestJsonRespones,MethodNotAllowedJsonRespones, NotFoudJsonResponr
from django.http import JsonResponse

def index(request):
    articles = Article.objects.all().select_related('author').prefetch_related('images', 'cate_id')
    # articles = Article.objects.filter(author__isnull=False, images__isnull=False, cate_id__isnull=False)
    return render(request, "index.html", {'articles': articles})


# def detadetailil(request,id):

#     return render(request,'detail.html')
#     
#     
def user_login(request):
    if request.method == 'POST':
        form =LoginForm(data=request.POST)
        if form.is_valid():
            form.do_login(request)
            print('表单验证通过')
            urls=reverse('index:info')
            # print(urls)
            return redirect(urls)
        else:
            print(form.errors)
    else: 
        form = LoginForm()
    
    return render(request,'login.html',{'form':form})


def user_logout(request):
    logout(request)
    return redirect(reverse('index:info'))


@login_required(login_url='/index/login/')
def info(request):
    # print(user)
    return render(request,'info.html')



def user_edit(request):
    if request.method == 'POST':
        form =UserForm(data=request.POST)
        if form.is_valid():
            print('get')
    else:
        form = UserForm()
    return render(request,'edit.html',{'form':form})


def register(request):
    
    if request.method == 'POST':
        data = request.POST
        # print(data)
    form = UserRegForm(data)
    if form.is_valid():
        form_data = form.cleaned_data
        # print(form_data)
    else:
        print(form.errors)
    return render(request,'register.html',{'form':form})



    '''登录退出接口'''
def api_login(request):

    #POST获取输入内容
    if request.method == 'POST':
        form = LoginForm(request.POST)
    #表单验证,#通过执行登录，
        if form.is_valid():
            user = form.do_login(request)
            print(user)
            data = {
                'username': user.username,
                'password': user.password
            }
            # serialized_data = json.dumps(data)
            return JsonResponse(data)
            # return http.jsonResponse(serialized_data,safe=False)
    #返回客户端，封装用户基本数据
        else: 
            # print("表单验证错误信息：", form.errors)  
    #每通过就返回错误信息
            data = {
                'code':'4000',
                'msg':'没通过！'
            }
            return JsonResponse(data)
            # serialized_data = json.dumps(data)
            # return JsonResponse(serialized_data,safe=False)
    else:
        # 请求不允许
        return MethodNotAllowedJsonRespones()